﻿using System;
using System.Text;
using Medianamik.Core.Querying;

namespace Medianamik.Core.DAL.SQL
{
    public class XmlColumnPredicate : Predicate
    {
        protected string DataType { get; set; }

        public XmlColumnPredicate(string pSubject, Comparison pComparison, IExpression pExpression, string dataType)
            : base(pSubject, pComparison, pExpression)
        {
            DataType = dataType;
        }

        //data.exist (''/ArrayOfNodePropertyValueLite/NodePropertyValueLite/PropertyId[.=sql:variable("@p2")]'')=1
        public override StringBuilder ToSQL()
        {
            var sql = new StringBuilder()
                .Append(@"T.c.value('(./" + Subject + @")[1]', '" + DataType + @"') ")
                .Append(Comparison.ToSqlOperator())
                .Append(Expression.ToSQL());

            return sql;
        }
    }
}